/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.oea.servlets;

import com.oea.hibernate.entities.Spresence;
import com.oea.hibernate.entities.Student;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author RANGER
 */
public class StudentViewPresenceAction extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            String id_str = request.getParameter("id");
            Long sid = Long.parseLong(id_str);
            
            List<Spresence> allpresence = Spresence.getPresenceByStudentId(sid);
            
            HashMap<String,String> allsubjects = new HashMap<String,String>();
            
            int total_presence=0,total_lectures=0;
            
            for (Spresence sp : allpresence) {
                sp.setLect(sp.getLid());
                sp.setStu(sp.getSid());
                sp.getLect().setSub(sp.getLect().getSubid());
                if(sp.getStatus()==1){
                    total_presence+=1;
                }
               
                total_lectures+=1;
                if(allsubjects.get(sp.getLect().getSub().getName())!=null){
                    String count_str=allsubjects.get(sp.getLect().getSub().getName()); // 1-2
                    String parts[]=count_str.split("/");
                    if(sp.getStatus()==1){
                        int count=Integer.parseInt(parts[0])+1; //2
                        int total=Integer.parseInt(parts[1])+1;
                        String final_str=count+"/"+total;
                        allsubjects.put(sp.getLect().getSub().getName(),final_str);
                        
                    }
                    else{
                        int count=Integer.parseInt(parts[0]); //2
                        int total=Integer.parseInt(parts[1])+1; 
                        String final_str=count+"/"+total;
                        allsubjects.put(sp.getLect().getSub().getName(),final_str);
                    }
                    
                    
                    
                    
                }else{
                       if(sp.getStatus()==1){
                            allsubjects.put(sp.getLect().getSub().getName(),"1/1");
                       }
                       else{
                            allsubjects.put(sp.getLect().getSub().getName(),"0/1");
                       }
                    
                }
            }
            
            request.getSession().setAttribute("avgpresence", (Float.parseFloat(""+total_presence)/total_lectures)*100);
            request.getSession().setAttribute("allsubjects", allsubjects);
            request.getSession().setAttribute("student", ((Student)new Student().getById(sid)));
            response.sendRedirect("student-view-presence.jsp");
            
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
